﻿using System;
using System.Collections.Generic;
using System.Text;

namespace xReplace
{
    class TextFragment
    {
        private string m_src = "";
        private string m_output = "";
        private int m_startIndex = 0;

        public TextFragment(string source)
        {
            m_src = source;
        }

        public TextFragment(string source, int startIndex)
        {
            m_src = source;
            m_startIndex = startIndex;      
        }

        public bool GrabFragment(string startsWith, string endsWith)
        {
            return GrabFragment(startsWith, endsWith, false);
        }

        public bool GrabFragment(string startsWith, string endsWith, bool ignoreCase)
        {
            return GrabFragment(startsWith, endsWith, "", "", ignoreCase);
        }

        public bool GrabFragment(string startsWith, string endsWith, string ignoreStart, string ignoreEnd)
        { 
            return GrabFragment(startsWith,endsWith,ignoreStart, ignoreEnd,false);
        }

        public bool GrabFragment(string startsWith, string endsWith, string ignoreStart, string ignoreEnd, bool ignoreCase)
        {
            if ((startsWith == "") && (endsWith == "") && (ignoreStart == "") && (ignoreEnd == ""))
            {
                m_output = m_src;
                return true;
            }

            m_output = "";
            int idxStart = 0;
            if (startsWith != "")
            {
                idxStart = m_src.IndexOf(startsWith, m_startIndex, ((ignoreCase) ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture));
                if (idxStart < 0) return false;
            }

            int idxEnd = m_src.Length - 1;
            if (endsWith != "")
            {
                idxEnd = m_src.IndexOf(endsWith, idxStart + startsWith.Length, ((ignoreCase) ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture));
                if (idxEnd < 0) return false;
            }
            string output = m_src.Substring(idxStart, (idxEnd + endsWith.Length) - idxStart);
            if(ignoreStart!="") output = IgnoreFragmentStart(output,ignoreStart, ignoreCase);
            if(ignoreEnd!="") output = IgnoreFragmentEnd(output,ignoreEnd, ignoreCase);
            m_output = output;
            m_startIndex = (idxEnd + endsWith.Length);
            return true;
        }

        private string IgnoreFragmentStart(string src, string ignore, bool ignoreCase)
        {
            return (src.StartsWith(ignore, ((ignoreCase) ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture))) ? src.Substring(ignore.Length) : src;

        }

        private string IgnoreFragmentEnd(string src, string ignore, bool ignoreCase)
        {
            return (src.EndsWith(ignore, ((ignoreCase) ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture))) ? src.Remove(src.Length - ignore.Length) : src;
        }

        public string Text 
        {
            get { return m_src; }
        }

        public string Fragment
        {
            get { return m_output; }
        }

        public int EndIndex
        {
            get { return m_startIndex; }
        }
    }
}
